// pages/validcode/validcode.js
import {my_request,my_showToast_error} from '../../utils/my_utils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input_code:'',
    judging:true,
    explain_str:wx.getStorageSync('valid_code_explain')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取授权信息
        //publisher-identities
        var that=this;
        wx.request({
          url:"https://sichuangyi.demopp.in/config/explain.json?timestamp="+Date.parse(new Date()),
          success: function(res){
            if(res.data.valid_code_explain){
              that.setData({
                explain_str:res.data.valid_code_explain
              })
              wx.setStorageSync('valid_code_explain');
            }
          },
        })  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
       
        
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  valid_code:function(value,length){
    return true
  },
  input_code:function(e){
    var value = e.detail.value;
    var length = e.detail.cursor
    var code = this.valid_code(value,length);
    if(!code){
      //未通过验证
    }
    this.setData({
      input_code:value
    })
  },
  submit_invitation:function(){
    var that=this;
    //通过验证提交
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/invitation-register',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        "authorization": "Bear " + wx.getStorageSync("auth_token")
      },
      method: "POST",
      data:{
        invitation_code:this.data.input_code
      },
      success: function (res) {
       
        //获取授权信息
        //publisher-identities
        my_request({
          url:"https://sichuangyi.demopp.in/api-conference-alpha/have-add-perm",
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          success: function(res){
            //that.load_script();
            wx.setStorageSync("has_add_perm",that.data.has_add_perm);
            var perm_list = [];
            var id_list=[]
            var data_list = res.data.data.list;
            for(var i=0;i<data_list.length;++i){
              var desc = data_list[i].contact_name+'\n'+data_list[i].contact_phone+"\n"+data_list[i].contact_org;
              perm_list.push(desc)
              id_list.push(data_list[i].id)
            }
            wx.setStorageSync("publisher-identities",perm_list);
            wx.setStorageSync("publisher-identities-ids",id_list);
            wx.showToast({
              title: '授权成功',
              icon: 'success',
              duration: 2000
            })
            wx.redirectTo({
              url:'/pages/conference/input_first'
            })
          },
          fail:function(){
            my_showToast_error("授权错误",2000)
          }
        })  
      },
      fail: function (res) {
        my_showToast_error("错误的验证码",2000)
      }
    })
  }
})